#include "CustomItem.h"

int row;
int colum;

CustomItemDelegrate::CustomItemDelegrate(const QRegExp& regExp,QObject* parent) : QStyledItemDelegate(parent)
{
    m_regExp = regExp;
    tbwig = qobject_cast<QTableWidget*>(parent);
}
CustomItemDelegrate::~CustomItemDelegrate(void)
{
}
QWidget* CustomItemDelegrate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    //创建带有正则表达式的输入框
    Q_UNUSED(option);
    Q_UNUSED(index);
    QLineEdit* editor = new QLineEdit(parent);
    connect(editor,SIGNAL(textChanged(QString)),this,SLOT(textChanged(QString)));

    //editor->setValidator(new QRegExpValidator(m_regExp, parent));
    QDoubleValidator* Dv = new QDoubleValidator(0.0,99.0,2, parent);
    Dv->setNotation(QDoubleValidator::StandardNotation);
    editor->setValidator(Dv);
    return editor;
}
void CustomItemDelegrate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
    QString text = index.model()->data(index, Qt::EditRole).toString();
    QLineEdit* lineEdit = qobject_cast <QLineEdit*>(editor);
    row = index.row();
    colum = index.column();
    //qDebug() << lineEdit->text();

    qDebug()<< text << "row:"<< index.row() << "colum"<<index.column();

    lineEdit->setText(text);

}
void CustomItemDelegrate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
    QLineEdit* lineEdit =  qobject_cast<QLineEdit*>(editor);
    QString text = lineEdit->text();
    qDebug()<< text;
    QRegExp rx("\\.");
    int pos = rx.indexIn(text);
    if(pos>=0){
        qDebug()<< "OK";
    }
    else {
        qDebug()<< "Error";
        if(text.length()>0)
            text.append(".0");
        else
            text.append("0.0");
        lineEdit->setText(text);
    }
    qDebug()<< text;
    model->setData(index, text, Qt::EditRole);
    QString *ss = new QString(text);
    //tbwig->item(index.row(),index.column())->setText("88");
}

bool CustomItemDelegrate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    bool ret;


    return ret;
}
bool CustomItemDelegrate::eventFilter(QObject *editor, QEvent *event)
{
    bool ret;

    return ret;
}

void CustomItemDelegrate::textChanged(QString ch)
{
    qDebug()<< ch;

    if(!ch.isEmpty())
        tbwig->item(row,colum)->setText(ch);

}
